<?php

namespace app\controller;

use app\BaseController;
use think\captcha\facade\Captcha;
use think\exception\ValidateException;
use app\services\AdminService;
use app\services\JWTService;

class Login extends BaseController
{
    /**
     * @description: 生成验证码
     * @param {*}
     * @return {*}
     */    
    public function getCaptcha()
    {
        $data = Captcha::create();
        return $data;
    }
    /**
     * @description: 登录验证
     * @param {*}
     * @return {*}
     */
    public function login(){
        $param = $this->request->param();
        //dd(app());
        try{
            $this->validate($param,'app\validate\Login');
        }catch(ValidateException $e){
            return $this->fail($e->getMessage());
        }

        if(!Captcha::check($param['captcha'])){
            return $this->fail('验证码验证失败');
        }

        unset($param['captcha']);
        $res = AdminService::checkAdmin($param);
       
        if($res['code']){
            return $this -> success($res['msg'],$res['data']);
        }else{
            return $this -> fail($res['msg']);
        }
    }
    

    public function testToken(){
        $token = $this->request->param('token');

        $token = trim(ltrim($token, 'Bearer'));

        // $token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiIhQCMkJSomIiwiYXVkIjoiIiwiaWF0IjoxNjI1ODIzNjEyLCJuYmYiOjE2MjU4MjM2MTUsImV4cCI6MTYyNTgzMDgxMiwiZGF0YSI6eyJ1aWQiOjEsImFjY291bnQiOiJhZG1pbiIsInJlYWxfbmFtZSI6IiIsInJvbGUiOiIxIn19.ze-LJ-pIq9Xtk9xdNaiFR9OTUvnr6DmxjNBym_0qqaQ";
        $res = JWTService::decode($token);
        dd($res);
    }

}
